﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PSE.Framework.UI.Controls.SmartClient
{
    /// <summary>
    /// Controle Expander extendido com funcionalidades adicionais de animacao
    /// </summary>
    public class Expander : System.Windows.Controls.Expander
    {
		ExpanderContainer container = null;

		public static readonly DependencyProperty AllowExpandProperty = DependencyProperty.Register("AllowExpand", typeof(Boolean), typeof(Expander), new FrameworkPropertyMetadata(true));
		public static readonly DependencyProperty VerticalSplitterProperty = DependencyProperty.Register("VerticalSplitter", typeof(VerticalGridSplitter), typeof(Expander), new FrameworkPropertyMetadata(OnVerticalSplitterChanged));
		public static readonly DependencyProperty HorizontalSplitterProperty = DependencyProperty.Register("HorizontalSplitter", typeof(HorizontalGridSplitter), typeof(Expander), new FrameworkPropertyMetadata(OnHorizontalSplitterChanged));
		public static readonly DependencyProperty IsPinnedProperty = DependencyProperty.Register("IsPinned", typeof(bool), typeof(Expander), new FrameworkPropertyMetadata(false, OnIsPinnnedChanged));

        public Expander()
        {
            this.Loaded += new RoutedEventHandler(Expander_Loaded);

            this.RowHeight = GridLength.Auto;
            this.RowMinHeight = 0;
        }
        
        static Expander()
        {
            IsExpandedProperty.OverrideMetadata(typeof(Expander), new FrameworkPropertyMetadata(true));
        }

        internal GridLength RowHeight
        {
            get;
            set;
        }

        internal double RowMinHeight
        {
            get;
            set;
        }

        public Boolean AllowExpand
        {
            get { return (Boolean)GetValue(AllowExpandProperty); }
            set { SetValue(AllowExpandProperty, value); }
        }

        public VerticalGridSplitter VerticalSplitter
        {
            get { return (VerticalGridSplitter)GetValue(VerticalSplitterProperty); }
            set { SetValue(VerticalSplitterProperty, value); }
        }

        public HorizontalGridSplitter HorizontalSplitter
        {
            get { return (HorizontalGridSplitter)GetValue(HorizontalSplitterProperty); }
            set { SetValue(HorizontalSplitterProperty, value); }
        }

        public bool IsPinned
        {
            get { return (bool)GetValue(IsPinnedProperty); }
            set { SetValue(IsPinnedProperty, value); }
        }

        private static void OnIsPinnnedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Expander obj = d as Expander;

            if (obj.VerticalSplitter != null)
                obj.VerticalSplitter.IsEnabled = !obj.IsPinned;

            if (obj.HorizontalSplitter != null)
                obj.HorizontalSplitter.IsEnabled = !obj.IsPinned;
        }

        private static void OnHorizontalSplitterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Expander obj = d as Expander;

            if (obj.HorizontalSplitter != null)
                obj.HorizontalSplitter.IsEnabled = !obj.IsPinned;
        }

        private static void OnVerticalSplitterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Expander obj = d as Expander;

            if (obj.VerticalSplitter != null)
                obj.VerticalSplitter.IsEnabled = !obj.IsPinned;

        }

        private void Expander_Loaded(object sender, RoutedEventArgs e)
        {
             container = ControlHelper.FindParent(this, typeof(ExpanderContainer)) as ExpanderContainer;

			 if (container == null)
				 throw new InvalidOperationException("Expander must be inside of ExpanderContainer.");
			 else
			 {
				 if (container.RowDefinitions.Count == 0)
				 {
					 RowDefinition row = new RowDefinition();
					 row.Height = new GridLength(1, GridUnitType.Star);
					 container.RowDefinitions.Add(row);
				 }
			 }
        }

        protected override void OnExpanded()
        {
            base.OnExpanded();
            ExpandMinimize();
        }
         
        protected override void OnCollapsed()
        {
            base.OnCollapsed();
            ExpandMinimize();
        }

        private void ExpandMinimize()
        {
            if (HorizontalSplitter != null)
                HorizontalSplitter.IsEnabled = this.IsExpanded && !this.IsPinned;

            container = ControlHelper.FindParent(this, typeof(ExpanderContainer)) as ExpanderContainer;

            if (container != null)
                container.SetRowDefinition(this);
        }
    }
}